/*
 * @Author: Hunter
 * @Date: 2022-03-10 22:59:59
 * @LastEditTime: 2022-05-01 17:34:36
 * @LastEditors: Hunter
 * @Description: 
 * @FilePath: \book_mark\src\utils\file.ts
 * 可以输入预定的版权声明、个性签名、空行等
 */
import type { UploadFile } from "element-plus/es/components/upload/src/upload.type";
export type readFileType = 'readAsArrayBuffer' | 'readAsBinaryString' | 'readAsDataURL' | 'readAsText'
export declare interface IFileSystem {
    readFile: (file: UploadFile, type?: readFileType, encoding?: string) => Promise<ProgressEvent<FileReader>>
    downloadFile: (url: string, name?: string) => void
    stringToBlobURL: (fileString: string) => string
}
export class FileSystem implements IFileSystem {
    readFile(file: UploadFile, type: readFileType = 'readAsText', encoding: string = 'utf-8') {
        const reader: FileReader = new FileReader();
        reader[type](file.raw, encoding)
        return new Promise((resolve: (e: ProgressEvent<FileReader>) => void) => {
            reader.onload = resolve
        })
    }
    downloadFile(url: string, name: string = 'file.txt') {
        const link = document.createElement('a')
        link.href = url
        link.download = name
        const _evt = new MouseEvent('click')
        link.dispatchEvent(_evt)
    }
    stringToBlobURL(fileString: string) {
        return URL.createObjectURL(new Blob([fileString], { type: "application/octet-stream" }))
    }
}